package com.nbpt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.nbpt.db.AccountDBHelper;

public class RegisterActivity extends AppCompatActivity {
    private EditText et_usernameReg,et_passwordReg,et_passwordRegg,et_phoneNum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Init();
    }
    public void Init(){
        et_usernameReg=findViewById(R.id.et_usernameReg);
        et_passwordReg=findViewById(R.id.et_passwordReg);
        et_passwordRegg=findViewById(R.id.et_passwordRegg);
        et_phoneNum=findViewById(R.id.et_phonrNum);
    }

    public void reg(View view) {
        String username=et_usernameReg.getText().toString();
        String password=et_passwordReg.getText().toString();
        String apassword=et_passwordRegg.getText().toString();
        String phoneNum=et_phoneNum.getText().toString();
        int gender=1;

        if ( !username.isEmpty() && !password.isEmpty() && !apassword.isEmpty() && !phoneNum.isEmpty()){
            if (password.equals(apassword))
            {

                AccountDBHelper accountDBHelper=new AccountDBHelper(RegisterActivity.this);
                long result=accountDBHelper.insertNewAccount(username,password,gender,phoneNum);
                if (result>0){
                    Toast.makeText(this,"注册成功！",Toast.LENGTH_LONG).show();
                    Intent intent =new Intent();
                    intent.putExtra("extra1",username);
                    intent.putExtra("extra2",password);
                    setResult(1,intent);
                    finish();
                }else if(result==-1){
                    Toast.makeText(this,"该账号已存在！",Toast.LENGTH_LONG).show();
                }

//            Toast.makeText(this,"用户名："+username+"密码："+password+"性别："+gender+"手机号码："+phoneNum,Toast.LENGTH_LONG).show();
            }
            else
            {
                Toast.makeText(this,"两次密码不一致,请重新输入！",Toast.LENGTH_LONG).show();
            }
        }
        else
        {
            Toast.makeText(this,"注册失败,请重新注册！",Toast.LENGTH_LONG).show();
        }
    }
}
